package com.jeesite.modules.digitalize.util.bimface;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.bimface.api.bean.request.integrate.FileIntegrateRequest;
import com.bimface.api.bean.request.integrate.IntegrateSource;
import com.bimface.api.bean.request.modelCompare.CompareRequest;
import com.bimface.api.bean.request.translate.FileTranslateRequest;
import com.bimface.api.bean.request.translate.TranslateSource;
import com.bimface.api.bean.response.FileIntegrateBean;
import com.bimface.api.bean.response.FileTranslateBean;
import com.bimface.api.bean.response.ModelCompareBean;
import com.bimface.data.bean.ModelCompareChange;
import com.bimface.data.bean.ModelCompareDiff;
import com.bimface.data.bean.Pagination;
import com.bimface.exception.BimfaceException;
import com.bimface.sdk.BimfaceClient;
import com.bimface.sdk.config.Endpoint;
import com.google.common.collect.Lists;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.ServiceException;
import com.jeesite.modules.digitalize.entity.DigitalizeComponentCompare;
import com.jeesite.modules.digitalize.entity.dto.DigitalizeComponentIntegrationDTO;
import com.jeesite.modules.digitalize.entity.dto.DigitalizeComponentIntegrationFileDTO;
import com.jeesite.modules.digitalize.util.word.compare.DiffTypeEnum;
import com.jeesite.modules.digitalize.util.word.compare.MyModelCompareChange;
import com.jeesite.modules.digitalize.util.word.compare.MyModelCompareDiff;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class BimFaceUtils {

    @Resource
    private BimFaceProperties bimFaceProperties;

    private BimfaceClient bimfaceClient;


    /**
     * 下面这个和上面的意思差不多，但是这个是新版本
     * 获取一个连接minio服务端的客户端
     *
     * @return MinioClient
     */
    @Bean
    public BimfaceClient getBimFaceClient() {
        Endpoint endpoint = new Endpoint();
        if (StringUtils.isNotBlank(bimFaceProperties.getUrl())) {
            endpoint.setApiHost(bimFaceProperties.getUrl());
        } else if (StringUtils.isNotBlank(bimFaceProperties.getIp()) && bimFaceProperties.getPort() != null) {
            String url = "http:" + bimFaceProperties.getIp() + ":" + bimFaceProperties.getPort();
            endpoint.setApiHost(url);
        }
        BimfaceClient bimfaceClient = new BimfaceClient(bimFaceProperties.getAppKey(), bimFaceProperties.getAppSecret(), endpoint);
        this.bimfaceClient = bimfaceClient;
        return bimfaceClient;
    }
    public ModelCompareBean compare(DigitalizeComponentCompare com) {
        try {
            CompareRequest compareRequest = new CompareRequest();
            compareRequest.setName(com.getName());
            compareRequest.setPreviousId(com.getOneComponentId());
            compareRequest.setFollowingId(com.getTwoComponentId());
            compareRequest.setCallback(bimFaceProperties.getCallbackUrl());
            compareRequest.setComparedEntityType("file");
            ModelCompareBean compareInfo = bimfaceClient.compareV2(compareRequest);
            return compareInfo;
        } catch (BimfaceException e) {
            throw new ServiceException(e);
        }
    }


    public FileTranslateBean convert(Long id) {
        try {
            FileTranslateRequest translate = new FileTranslateRequest();
            TranslateSource source = new TranslateSource();
            source.setFileId(Long.valueOf(id));
            source.setCompressed(false);
            translate.setSource(source);
            translate.setCallback(bimFaceProperties.getCallbackUrl());
            FileTranslateBean bean = bimfaceClient.translate(translate);
            return bean;
        } catch (BimfaceException e) {
            throw new ServiceException(e);
        }
    }

    public Integer getComStatus(Long compareId) {
        try {
            ModelCompareBean compareInfo = bimfaceClient.getCompareInfo(compareId);
            String status = compareInfo.getStatus();
            if (status.equals("processing")) {
                return 0;
            } else if (status.equals("success")) {
                return 1;
            } else  {
                return -1;
            }
        } catch (BimfaceException e) {
            throw new RuntimeException(e);
        }
    }

    public List<MyModelCompareDiff> getCompareData(Long compareId) {
        try {
            Pagination<ModelCompareDiff> modelCompareResult = bimfaceClient.getModelCompareResult(compareId, null, null, 1, Integer.MAX_VALUE);
            List<MyModelCompareDiff> data = Lists.newArrayList();
            for (ModelCompareDiff comf : modelCompareResult.getData()) {
                MyModelCompareDiff datum = BeanUtil.copyProperties(comf, MyModelCompareDiff.class);
                if (datum.getDiffType().equals(DiffTypeEnum.CHANGE.getCode())) {
                    ModelCompareChange modelCompareElementChange = bimfaceClient.getModelCompareElementChange(compareId, Long.valueOf(datum.getPreviousFileId()), datum.getElementId(), Long.valueOf(datum.getFollowingFileId()), datum.getElementId());
                    String jsonString = JSON.toJSONString(modelCompareElementChange);
                    datum.setDiffData(JSON.parseObject(jsonString, MyModelCompareChange.class));
                }
                data.add(datum);
            }
            return data;
        } catch (BimfaceException e) {
            throw new RuntimeException(e);
        }
    }

    public FileIntegrateBean integration(DigitalizeComponentIntegrationDTO dto) {
        try {
            FileIntegrateRequest request = new FileIntegrateRequest();
            List<IntegrateSource> list = new ArrayList<>();
            for (DigitalizeComponentIntegrationFileDTO fileMsg : dto.getFileList()) {
                IntegrateSource source = new IntegrateSource();
                source.setFileId(fileMsg.getFileId());
                source.setFileName(fileMsg.getFileName());
                list.add(source);
            }
            request.setName(dto.getName());
            request.setSources(list);
            request.setCallback(bimFaceProperties.getCallbackUrl());
            FileIntegrateBean integrate = bimfaceClient.integrate(request);
            return integrate;
        } catch (BimfaceException e) {
            throw new ServiceException(e);
        }
    }
}
